package com.baseweb.webadmin.weixin.jssdk;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import com.baseweb.webadmin.core.util.ToolHelper;
import com.baseweb.webadmin.weixin.sign.ConfingJsSign;
import com.baseweb.webadmin.weixin.utils.HttpGetOrPost;

/**
 * 微信地理位置定位
 * @author wcc
 *
 */
@Controller
@RequestMapping(value = "/geolocation")
public class Geolocation {
	private Logger logger=LoggerFactory.getLogger(Geolocation.class);
	private String ak ="jLb4hKENGz1esZPH0WTtHXxk";
	/**
	 * 获取微信CofingJs签名后的数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/cofingJs")
	public ModelAndView     getCofingJs(HttpServletRequest request ){
		ModelAndView  modelView =  new 	ModelAndView("weixin/jsp/geolocation");
	    Map<String, String> map =   ConfingJsSign.getConfingJsSign( request);
	    modelView.addObject("map",map);
	  logger.info("定位用户信息"+map);
	  return modelView;
	}
	/**
	 * 根据经纬度获取其所对应城市信息<br>
	 * 默认是成都市
	 * @param request  
	 */
	@RequestMapping(value="/positionCity")
	public  ModelAndView  positionCity(HttpServletRequest request){
		ModelAndView view = new ModelAndView("weixin/jsp/home");
		  String latitude =  request.getParameter("latitude");
		  String longitude =  request.getParameter("longitude");
		  logger.info("用户经纬度"+latitude+"---->"+longitude);
		  Map<String, String> map = new HashMap<String, String>();
 		  if(ToolHelper.isEmpty(latitude) || ToolHelper.isEmpty(longitude) ){
 			 logger.info("用户经纬度位空，采用默认值");
			  //定位失败 默认成都市
 			  map.put("provinceName", "四川省");
 			  map.put("cityName", "眉山市");
 			  view.addObject("map",map);
			  return view;
		  }
		  StringBuffer stringBuffer = new StringBuffer("http://api.map.baidu.com/geocoder/v2/?");
		  stringBuffer.append("ak=").append(ak).append("&location=")
    	 .append(latitude).append(",").append(longitude).append("&output=json&pois=0&coordtype=wgs84ll"); 
    	  JSONObject city =  HttpGetOrPost.sendGet(stringBuffer.toString());
    	  /**
    	   * {"status":0,
    	   * "result":
    	   * {"location":
    	   * {"lng":104.07736977456,"lat":30.538779318637},
    	   * "formatted_address":"四川省成都市双流县天府大道中段","business":"华阳",
    	   * "addressComponent":
    	   * {"city":"成都市",
    	   * "country":"中国","direction":"","distance":"",
    	   * "district":"双流县",
    	   * "province":"四川省","street":"天府大道中段","street_number":"","country_code":0},
    	   * "poiRegions":[],"sematic_description":"九点时光会所附近30米","cityCode":75}}
    	   */
    	  String provinceName = (String) city.getJSONObject("result").getJSONObject("addressComponent").get("province");
    	  String cityName = (String) city.getJSONObject("result").getJSONObject("addressComponent").get("city");
    	  String araeName = (String) city.getJSONObject("result").getJSONObject("addressComponent").get("district");
    	  logger.info("用户城市"+cityName+"---->"+provinceName);
    	  map.put("provinceName",provinceName);
		  map.put("cityName", cityName);
		  map.put("araeName", araeName);
		  //经纬度放入map中
		  map.put("latitude", latitude);
		  map.put("longitude", longitude);
		  view.addObject("map",map);
    	  return  view; 
	}
       
}
